#define _CRT_SECURE_NO_WARNINGS 1
#include <cstdio>
#include <cmath>
#include <algorithm>
  struct TreeNode {
      int val;
      struct TreeNode *left;
      struct TreeNode *right;
  };
 
int max(int a, int b)
{
    return a > b ? a : b;
}
int is(struct TreeNode* root)
{
    if (!root) return 0;
    return max(is(root->left), is(root->right)) + 1;
}
bool isBalanced(struct TreeNode* root) {
    int a, b = 0;
    if (root->left)
        a = is(root->left);
    if (root->right)
        b = is(root->right);
    return abs(a - b) <= 1;
}